package cn.example.demo.modules.house.recommend;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Description: 行为权重计算器
 *
 * @author Lzx
 * @create 2025/3/22 16:14
 */
@Component
public class BehaviorWeightCalculator {
    // 基础权重配置
    private static final Map<BehaviorType, Float> BASE_WEIGHTS = MapUtil.of(
            Pair.of(BehaviorType.VIEW, BehaviorType.VIEW.getWeight()),
            Pair.of(BehaviorType.COLLECT, BehaviorType.COLLECT.getWeight()),
            Pair.of(BehaviorType.ORDER, BehaviorType.ORDER.getWeight()),
            Pair.of(BehaviorType.RATE, BehaviorType.RATE.getWeight())
    );

    // 动态调整权重
    public float calculate(BehaviorType type, JsonNode extra) {
        float base = BASE_WEIGHTS.getOrDefault(type, 0.5f);
        // 评分影响
        if (type == BehaviorType.RATE && extra.has("score")) {
            int score = extra.get("score").asInt(5);
            return base * (score / 5.0f);
        }
        // 订单金额影响
        if (type == BehaviorType.ORDER && extra.has("amount")) {
            float amount = extra.get("amount").floatValue();
            return base * Math.min(1.0f, amount / 1000.0f);
        }
        return base;
    }
}
